#ifndef TEST2_H
#define TEST2_H


#include "qcustomplot.h"
#include <QHash>

class DynamicPixmapBinder : public QObject {
    Q_OBJECT
public:
    struct Connection {
        QCPItemLine* upperLine; // 上连接线（右上→左上）
        QCPItemLine* lowerLine; // 下连接线（右下→左下）
    };

    DynamicPixmapBinder(QCustomPlot *plot) : mCustomPlot(plot)
    {

    }

    // 添加关联
    void addConnection(QCPItemRect* rect, QCPItemPixmap* pixmap) {
        if (!rect || !pixmap) return;

        // 创建连接线
        Connection conn;
        conn.upperLine = createLine(QPointF(rect->bottomRight->key(), rect->topLeft->value()), pixmap->topLeft);
        conn.lowerLine = createLine(rect->bottomRight->coords(), pixmap->bottomLeft);

        mConnections[{rect, pixmap}] = conn;

        // 监听对象销毁事件
//        connect(rect, &QObject::destroyed, this, [this, rect]() { removeConnection(rect); });
//        connect(pixmap, &QObject::destroyed, this, [this, pixmap]() { removeConnection(pixmap); });
    }

//    explicit DynamicPixmapBinder(QCustomPlot* plot) : plot_(plot) {
//        connect(plot_, &QCustomPlot::afterReplot, this, &DynamicPixmapBinder::updateBindings);
//    }

//    // 绑定矩形到图片
//    void bindRectToPixmap(QCPItemRect* rect, QCPItemPixmap* pixmap) {
//        if (!rect || !pixmap) return;

//        // 建立锚点绑定（关键代码）
//        pixmap->topLeft->setParentAnchor(rect->topRight);     // 矩形右上→图片左上
//        pixmap->bottomRight->setParentAnchor(rect->bottomLeft);// 矩形左下→图片右下

//        QCPItemLine *arrow = new QCPItemLine(plot_);
//        arrow->start->setParentAnchor();
//        arrow->end->setCoords(4, 1.6); // point to (4, 1.6) in x-y-plot coordinates

//        // 记录绑定关系
//        bindings_[rect] = pixmap;
//        connect(rect, &QObject::destroyed, this, [this, rect](){ unbind(rect); });
//    }

//private slots:
//    void updateBindings() {
//        foreach(auto rect, bindings_.keys()) {
//            // 自动检测重叠并隐藏图片（可选功能）
//            bool overlap = checkOverlap(rect, bindings_[rect]);
//            bindings_[rect]->setVisible(!overlap);
//        }
//    }

private:
//    bool checkOverlap(QCPItemRect* rect, QCPItemPixmap* pixmap) {
//        // 计算轴坐标范围
//        QCPRange rectX(rect->topLeft->key(), rect->bottomRight->key());
//        QCPRange rectY(rect->bottomRight->value(), rect->topLeft->value());
//        QCPRange pixX(pixmap->topLeft->key(), pixmap->bottomRight->key());
//        QCPRange pixY(pixmap->bottomRight->value(), pixmap->topLeft->value());

//        // 判断坐标重叠
////        return rectX.intersects(pixX) && rectY.intersects(pixY);
//        return false;
//    }

//    void unbind(QCPItemRect* rect) {
//        if (bindings_.contains(rect)) {
//            // 解除锚点绑定
//            bindings_[rect]->topLeft->setParentAnchor(nullptr);
//            bindings_[rect]->bottomRight->setParentAnchor(nullptr);
//            bindings_.remove(rect);
//        }
//    }

    // 创建单条连接线
    QCPItemLine* createLine(const QPointF &start, QCPItemAnchor *end) {
        QCPItemLine* line = new QCPItemLine(qobject_cast<QCustomPlot*>(mCustomPlot));
        //line->start->setParentAnchor(start); // 绑定起点锚点
        line->start->setCoords(start);
        line->end->setParentAnchor(end);     // 绑定终点锚点
        line->setPen(QPen(Qt::darkGray, 1, Qt::DashLine));
        return line;
    }

    QCustomPlot* mCustomPlot;
    QHash<QPair<QCPItemRect*, QCPItemPixmap*>, Connection> mConnections;
};


#endif // TEST2_H
